package com.jsy.imagepicker;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.piasy.biv.indicator.progresspie.ProgressPieIndicator;
import com.github.piasy.biv.view.BigImageView;
import com.github.piasy.biv.view.FrescoImageViewFactory;

import java.io.File;

public class PreviewAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return PickerAdapter.allList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        String item = PickerAdapter.allList.get(position);

        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_preview, null);
        container.addView(itemView);
        BigImageView ivItem = itemView.findViewById(R.id.iv_item);
        ImageView ivGif = itemView.findViewById(R.id.iv_gif);
        if (PickerUtils.isGifFile(new File(item))){
            ivGif.setVisibility(View.VISIBLE);
            ivItem.setVisibility(View.GONE);
            Glide.with(container.getContext()).load(item).into(ivGif);
        }else {
            ivGif.setVisibility(View.GONE);
            ivItem.setVisibility(View.VISIBLE);
            ivItem.showImage(Uri.fromFile(new File(item)));
            ivItem.setImageViewFactory(new FrescoImageViewFactory());
            ivItem.setProgressIndicator(new ProgressPieIndicator());
        }
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


}
